package hn.cch.framework.google.guava;

import com.google.common.collect.Lists;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnel;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.google.common.hash.PrimitiveSink;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

/**
 * Guava:Google used java
 */
public class GuavaTest {

    private static final Logger logger = LoggerFactory.getLogger(GuavaTest.class);


    @Test
    public void test() {

        List<String> list = Lists.newArrayList("0", "1", "2");
        // Preconditions
        // Reflection

    }

    @Test
    public void testConcurrent() {
        ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(
            Executors.newFixedThreadPool(10));
        // ListenableFuture替换Future
        ListenableFuture<String> stringListenableFuture = listeningExecutorService.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "call";
            }
        });
        Futures.addCallback(stringListenableFuture, new FutureCallback<String>() {
            @Override
            public void onSuccess(@Nullable String s) {
                System.out.println(s);
            }

            @Override
            public void onFailure(Throwable throwable) {
                System.out.println(throwable.getMessage());
            }
        }, listeningExecutorService);

    }

    @Test
    public void testIO() {
        // ByteStreams
    }

    @Test
    public void testHash() {

        HashFunction hashFunction = Hashing.sha256();
        HashCode hashCode = hashFunction.newHasher().putString("10", StandardCharsets.UTF_8).putInt(10).hash();
        System.out.println(hashCode.toString());

        // 漏斗
        Funnel<Person> personFunnel = new Funnel<Person>() {
            @Override
            public void funnel(Person person, PrimitiveSink primitiveSink) {
                primitiveSink.putString(person.getName(), StandardCharsets.UTF_8).putInt(person.getAge());
            }
        };
        // 误判率0.01
        BloomFilter<Person> personBloomFilter = BloomFilter.create(personFunnel, 100, 0.01);
        for (int i = 0; i < 100; i++) {
            personBloomFilter.put(new Person(String.valueOf(i), i));
        }
        System.out.println(personBloomFilter.mightContain(new Person("100", 100)));

    }


    @Test
    public void testEventBus() {
        // 事件总线
        EventBus eventBus = new EventBus("test");
        // 注册监听者
        eventBus.register(new Listener());

        eventBus.post(new Message("message content"));

    }


}

class Message {
    private String content;

    public Message(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

class Listener {

    @Subscribe
    public void subscribe(Message message) {
        System.out.println(message.getContent());
    }
}

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }
}